<?php
declare (strict_types=1);

namespace app\service\logic\backend\system;

use app\model\communal\UserExpertModel;
use app\service\logic\BaseService;
use think\facade\Db;

final class CustomerService extends BaseService
{
    protected string $modelClassName = 'AdminModel';

    /**
     * 给专家分配客服
     *
     * @param $params
     * @return UserExpertModel
     */
    public function expert($params)
    {
        $id = $params['id'];
        $expertIds = explode(',', $params['expert_ids']);

        // 先取消后设置
        Db::connect('communal')->transaction(function () use ($id, $expertIds, &$result) {
            UserExpertModel::where('customer_id', $id)->update(['customer_id' => 0]);
            $result = UserExpertModel::whereIn('id', $expertIds)->update(['customer_id' => $id]);
        });
        return $result;
    }

    /**
     * 获取专家id根据客服id
     *
     * @param $customerId
     * @return array
     */
    public function getExpertIdsBy($customerId)
    {
        return UserExpertModel::where('customer_id', $customerId)->column('id');
    }
}
